package user

import (
	"context"
	"fmt"
	"github.com/google/uuid"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	//"github.com/gogf/gf-demo-user/v2/internal/model"
	"github.com/gogf/gf-demo-user/v2/internal/model/entity"
	"github.com/gogf/gf-demo-user/v2/api/web/v1"
	"github.com/gogf/gf-demo-user/v2/internal/service"
)

func (c *ControllerV1) User(ctx context.Context, req *v1.UserReq) (res *v1.UserRes, err error) {
	res = &v1.UserRes{
		service.User().GetUser(ctx),
	}
	return
}

func (c *ControllerV1) UserSubscription(ctx context.Context, req *v1.UserSubscriptionReq) (res *v1.UserSubscriptionRes, err error) {
	res = &v1.UserSubscriptionRes{
		service.User().GetUserSubscription(ctx),
	}
	return
}

func (c *ControllerV1) UserPost(ctx context.Context, req *v1.UserPostReq) (res *v1.UserPostRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	newUUID := uuid.New()
	id, err := g.Model("User").Data(g.Map{
		"id" : newUUID,
		"name" : req.Name,
		"description" : req.Description,
		"industry" : req.Industry,
		"tag" : req.Tag,
		"address" : req.Address,
		"website" : req.Website,
		"city" : req.City,
		"size" : req.Size,
		"contact" : req.Contact,
		"account" : account.Id,
		"logo" : req.Logo,
	}).Insert()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserMeDele(ctx context.Context, req *v1.UserMeDeleReq) (res *v1.UserMeDeleRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("UserSubscription").Where("User_id=?", account.Id).Delete()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserUp(ctx context.Context, req *v1.UserUpReq) (res *v1.UserUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("User").Data(g.Map{
		"name" : req.Name,
		"description" : req.Description,
		"industry" : req.Industry,
		"tag" : req.Tag,
		"address" : req.Address,
		"website" : req.Website,
		"city" : req.City,
		"size" : req.Size,
		"contact" : req.Contact,
		"account" : account.Id,
		"logo" : req.Logo,
	}).Where("id", account.Id).Update()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserMeUp(ctx context.Context, req *v1.UserMeUpReq) (res *v1.UserRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("User").Data(g.Map{
		"name" : req.Name,
		"description" : req.Description,
		"industry" : req.Industry,
		"tag" : req.Tag,
		"address" : req.Address,
		"website" : req.Website,
		"city" : req.City,
		"size" : req.Size,
		"contact" : req.Contact,
		"account" : account.Id,
		"logo" : req.Logo,
	}).Where("id", account.Id).Update()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserSubscriptionUp(ctx context.Context, req *v1.UserSubscriptionUpReq) (res *v1.UserSubscriptionUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("UserSubscription").Data(g.Map{
		"User_plan" : req.User_plan,
		"status" : req.Status,
		"expiry" : req.Expiry,
	}).Where("id", account.Id).Update()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserSubscriptionPost(ctx context.Context, req *v1.UserSubscriptionPostReq) (res *v1.UserSubscriptionUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	newUUID := uuid.New()
	id, err := g.Model("UserSubscription").Data(g.Map{
		"id" : newUUID,
		"User_id" : account.Id,
		"User_plan" : req.User_plan,
		"status" : req.Status,
		"expiry" : req.Expiry,
	}).Insert()
	fmt.Println(id)
	return
}


func (c *ControllerV1) UserResumes(ctx context.Context, req *v1.UserResumeReq) (res *v1.UserResumeRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	var user *entity.Resume
	err = g.Model("Resume").Where("company_id=?", account.Id).Scan(&user)
	fmt.Println(err)
	res = &v1.UserResumeRes{
		user,
	}
	return
}

func (c *ControllerV1) UserResume(ctx context.Context, req *v1.UserResumeReq) (res *v1.UserResumeRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	var user *entity.Resume
	err = g.Model("Resume").Where("company_id=?", req.Resume_id).Scan(&user)
	fmt.Println(err)
	res = &v1.UserResumeRes{
		user,
	}
	return
}

func (c *ControllerV1) UserResumeUp(ctx context.Context, req *v1.UserResumeUpReq) (res *v1.UserResumeUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("Resume").Data(g.Map{
		"media" : req.Media,
	}).Where("id", account.Id).Update()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserResumePost(ctx context.Context, req *v1.UserResumePostReq) (res *v1.UserResumeUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	newUUID := uuid.New()
	id, err := g.Model("Resume").Data(g.Map{
		"id" : newUUID,
		"media" : req.Media,
	}).Insert()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserResumeDele(ctx context.Context, req *v1.UserResumeDeleReq) (res *v1.UserResumeUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("Resume").Where("id=?", req.Resume_id).Delete()
	fmt.Println(id)
	return
}


func (c *ControllerV1) UserFirstcertUp(ctx context.Context, req *v1.UserFirstcertUpReq) (res *v1.UserResumeUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("Resume").Data(g.Map{
		"media" : req.Media,
	}).Where("id", account.Id).Update()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserFirstcert(ctx context.Context, req *v1.UserFirstcertReq) (res *v1.UserResumeRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	var user *entity.Resume
	err = g.Model("Firstcert").Where("company_id=?", account.Id).Scan(&user)
	fmt.Println(err)
	res = &v1.UserResumeRes{
		user,
	}
	return
}

func (c *ControllerV1) UserPictureUp(ctx context.Context, req *v1.UserPictureUpReq) (res *v1.UserResumeUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("Resume").Data(g.Map{
		"media" : req.Media,
	}).Where("id", account.Id).Update()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserPicturePost(ctx context.Context, req *v1.UserPicturePostReq) (res *v1.UserResumeUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	newUUID := uuid.New()
	id, err := g.Model("Resume").Data(g.Map{
		"id" : newUUID,
		"media" : req.Media,
	}).Insert()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserPictureDele(ctx context.Context, req *v1.UserPictureDeleReq) (res *v1.UserResumeUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("Resume").Where("id=?", req.Resume_id).Delete()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserSkill(ctx context.Context, req *v1.UserSkillReq) (res *v1.UserSkillRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	var user *entity.Skill
	err = g.Model("Skill").Where("id", req.Skill_id).Scan(&user)
	fmt.Println(err)
	res = &v1.UserSkillRes{
		user,
	}
	return
}

func (c *ControllerV1) UserSkillUp(ctx context.Context, req *v1.UserSkillUpReq) (res *v1.UserSkillUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("Skill").Data(g.Map{
		"name" : req.Name,
	}).Where("id", req.Skill_id).Update()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserSkillPost(ctx context.Context, req *v1.UserSkillPostReq) (res *v1.UserSkillUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	newUUID := uuid.New()
	id, err := g.Model("Skill").Data(g.Map{
		"id" : newUUID,
		"name" : req.Name,
	}).Insert()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserSkillDele(ctx context.Context, req *v1.UserSkillDeleReq) (res *v1.UserSkillUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("Skill").Where("id", req.Skill_id).Delete()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserEducation(ctx context.Context, req *v1.UserEducationReq) (res *v1.UserEducationRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	var user *entity.Education
	err = g.Model("Education").Where("id=?", req.Education_id).Scan(&user)
	fmt.Println(err)
	res = &v1.UserEducationRes{
		user,
	}
	return
}

func (c *ControllerV1) UserEducationUp(ctx context.Context, req *v1.UserEducationUpReq) (res *v1.UserEducationUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("Education").Data(g.Map{
		"start_date" : req.Start_date,
		"end_date" : req.End_date,
		"institute" : req.Institute,
		"institute_string" : req.Institute_string,
		"level" : req.Level,
		"programme" : req.Programme,
		"description" : req.Description,
	}).Where("id", req.Education_id).Update()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserEducationPost(ctx context.Context, req *v1.UserEducationPostReq) (res *v1.UserEducationUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	newUUID := uuid.New()
	id, err := g.Model("Education").Data(g.Map{
		"id" : newUUID,
		"start_date" : req.Start_date,
		"end_date" : req.End_date,
		"institute" : req.Institute,
		"institute_string" : req.Institute_string,
		"level" : req.Level,
		"programme" : req.Programme,
		"description" : req.Description,
	}).Insert()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserEducationDele(ctx context.Context, req *v1.UserEducationDeleReq) (res *v1.UserEducationUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("Education").Where("id=?", req.Education_id).Delete()
	fmt.Println(id)
	return
}


func (c *ControllerV1) UserExperience(ctx context.Context, req *v1.UserExperienceReq) (res *v1.UserExperienceRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	var user *entity.Experience
	err = g.Model("Experience").Where("id", req.Experience_id).Scan(&user)
	fmt.Println(err)
	res = &v1.UserExperienceRes{
		user,
	}
	return
}

func (c *ControllerV1) UserExperienceUp(ctx context.Context, req *v1.UserExperienceUpReq) (res *v1.UserExperienceUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("Experience").Data(g.Map{
		"user_id" : req.User_id,
		"start_date" : req.Start_date,
		"end_date" : req.End_date,
		"isPresent" : req.IsPresent,
		"description" : req.Description,
		"title" : req.Title,
		"company" : req.Company,
		"company_string" : req.Company_string,
	}).Where("id", req.Experience_id).Update()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserExperiencePost(ctx context.Context, req *v1.UserExperiencePostReq) (res *v1.UserExperienceUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	newUUID := uuid.New()
	id, err := g.Model("Experience").Data(g.Map{
		"id" : newUUID,
		"user_id" : req.User_id,
		"start_date" : req.Start_date,
		"end_date" : req.End_date,
		"isPresent" : req.IsPresent,
		"description" : req.Description,
		"title" : req.Title,
		"company" : req.Company,
		"company_string" : req.Company_string,
	}).Insert()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserExperienceDele(ctx context.Context, req *v1.UserExperienceDeleReq) (res *v1.UserExperienceUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("Experience").Where("id", req.Experience_id).Delete()
	fmt.Println(id)
	return
}

func (c *ControllerV1) UserList(ctx context.Context, req *v1.ListReq) (res *v1.ListRes, err error) {
	users := service.User().GetList(ctx)
	fmt.Println(users)
	res = &v1.ListRes{
	 	Users: users,
	}
	return
}


